/** @odoo-module **/

import { DateTimeField, dateTimeField } from "@web/views/fields/datetime/datetime_field";
import { formatDateTime, formatDate } from "@web/core/l10n/dates";
import { registry } from "@web/core/registry";

// 定义自定义的 DateTimeField 类
class CustomDateTimeField extends DateTimeField {
    static props = {
        ...DateTimeField.props,
        format: { type: String, optional: true }, // 添加自定义的 format 属性
    };

    setup() {
        super.setup();

        // 初始化格式化选项
        this.formatOptions = {
            format: this.props.format || "yyyy-MM-dd", // 默认格式
        };
    }

    getFormattedValue(valueIndex) {
        const value = this.values?.[valueIndex];

        if (!value) {
            return ""; // 如果值为空，返回空字符串
        }

        return this.field.type === "date"
            ? formatDate(value)
            : formatDateTime(value, this.formatOptions); // 使用动态格式化选项
    }
}

const customDateTimeField = {
    ...dateTimeField,
    component: CustomDateTimeField,


    extractProps: ({ attrs, options }) => ({
        format: options?.format || "YYYY/MM/DD HH:mm:ss", // 默认格式
    }),
};

registry.category("fields").add("custom_datetime", customDateTimeField);